
Partial Class PasswordRetrieval
    Inherits System.Web.UI.Page
    'Dim rettype As String

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Response.Cache.SetCacheability(HttpCacheability.NoCache)
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session("type") <> "" Then
            Response.Redirect("Error.aspx")
            Exit Sub
        End If

        pnlDetails.Enabled = False

        Session("retrieve") = ""
    End Sub

    Protected Sub btnGo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGo.Click
        pnlDetails.Enabled = True
        Dim type As String
        type = Left(ddlType.Text, 5)
        type = LCase(type)
        Session("retrieve") = type
        MsgBox("Go: " & Session("retrieve"), , "Go")



        If Session("retrieve") = "user" Then
            txtEmail.Enabled = True
            lblEmail.Enabled = True
            txtSecQuest.Enabled = True
            lblSecQuest.Enabled = True
            txtAns.Enabled = True
            lblAns.Enabled = True

            'Dim a As New Globo
            'a.executesql("select s_ans from users where uid='" & txtUname.Text & "'", False)
            'txtSecQuest.Text = a.dr(0)

        Else 'If Session("retrieve") = "admin" Or Session("retrieve") = "buyer" Then
            txtSecQuest.Enabled = False
            lblSecQuest.Enabled = False
            txtAns.Enabled = False
            lblAns.Enabled = False
            If Session("retrieve") = "admin" Then
                txtEmail.Enabled = False
                lblEmail.Enabled = False
            ElseIf Session("retrieve") = "buyer" Then
                txtEmail.Enabled = True
                lblEmail.Enabled = True
            End If
        End If


        ddlType.Enabled = False
        btnGo.Enabled = False
    End Sub

    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        MsgBox("ddl: " & ddlType.Text, , "ddl")
        Dim type As String
        type = Left(ddlType.Text, 5)
        type = LCase(type)
        Session("retrieve") = type
        MsgBox("Save: " & Session("retrieve"), , "Save")

        Dim uname, email, secq, ans As String
        uname = txtUname.Text
        email = txtEmail.Text
        secq = txtSecQuest.Text
        ans = txtAns.Text


        'Blank Filed Validation Check
        '++++++++++++++++++++++++++++++++++++++++++
        If txtUname.Text = "" Then
            lblErr.Text = "Enter User Name"
            lblErr.Visible = True
            Exit Sub
        End If

        If txtEmail.Text = "" And Session("retrieve") <> "admin" Then
            lblErr.Text = "Enter Email ID"
            lblErr.Visible = True
            Exit Sub
        End If

        If txtSecQuest.Text = "" And Session("retrieve") = "user" Then
            lblErr.Text = "Enter Security Question"
            lblErr.Visible = True
            Exit Sub
        End If

        If txtAns.Text = "" And Session("retrieve") = "user" Then
            lblErr.Text = "Enter Answer to Security Question"
            lblErr.Visible = True
            Exit Sub
        End If

        'If Right(email, 4) <> ".com" And Right(email, 6) <> ".co.in" Then
        '    lblErr.Text = "Enter valid User Name/Email ID"
        '    lblErr.Visible = True
        '    Exit Sub
        'End If






        '________________________________________________ validation
        Dim a As New Globo
        If Session("retrieve") = "admin" Then
            a.executesql("select * from admin where adminid='" & uname & "'", False)
        ElseIf Session("retrieve") = "user" Then
            a.executesql("select * from users where uid='" & uname & "' and uemail='" & email & "' and sec_quest='" & secq & "' and s_ans='" & ans & "'", False)
        ElseIf Session("retrieve") = "buyer" Then
            a.executesql("select * from buyers where buyer_id='" & uname & "' and bemail='" & email & "'", False)
        End If

        'Dim drrd As Boolean
        'drrd = a.dr.Read
        'If drrd = False Then
        If a.dr.Read = False Then
            MsgBox("a.dr.Read: " & a.dr.Read, , "inside If")
            lblErr.Text = "Enter all fields correctly for validation"
            If Session("retrieve") = "admin" Then lblErr.Text = "Enter valid Admin ID"
            lblErr.Visible = True
            Response.Redirect("Home.aspx")
            Exit Sub
        End If




        '________________________________________________ updation
        Dim b As New Globo
        Dim npswd As String
        npswd = "kdrsvrkd" 'generate new pswd here by some formula/algo
        If Session("retrieve") = "admin" Then
            b.executesql("update admin set adminpass='" & npswd & "' where adminid='" & uname & "'", True)
        ElseIf Session("retrieve") = "user" Then
            b.executesql("update users set pass='" & npswd & "' where uid='" & uname & "'", True)
        ElseIf Session("retrieve") = "buyer" Then
            b.executesql("update buyers set bpassword='" & npswd & "' where buyer_id='" & uname & "'", True)
        End If
        MsgBox("New password = " & npswd, MsgBoxStyle.OkOnly, "New password") 'disable


        MsgBox("You'll be informed about the new password via email!", MsgBoxStyle.OkOnly, "Password retrieval confirmation")
        Response.Redirect("Home.aspx")
    End Sub

    Protected Sub lnkAboutUs_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkAboutUs.Click
        Response.Redirect("AboutUs.aspx")
    End Sub

    Protected Sub lnkTermCond_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkTermCond.Click
        Response.Redirect("TermsConditions.aspx")
    End Sub

    Protected Sub lnkContactUs_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkContactUs.Click
        Response.Redirect("ContactUs.aspx")
    End Sub

    Protected Sub lnkFAQs_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFAQs.Click
        Response.Redirect("FAQs.aspx")
    End Sub

    Protected Sub lnkFeedback_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFeedback.Click
        Response.Redirect("Feedback.aspx")
    End Sub
End Class